﻿using GN.Pay.TradeChannel.DefaultImpl;
using GN.Pay.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GN.Pay.TradeChannel.UnionPay
{
    /// <summary>
    /// 付款通知响应实体
    /// </summary>
    [Serializable]
    public class PayNotifyResponseEntity
    {
        /// <summary>
        /// 证书ID
        /// </summary>
        public string certId { get; set; }

        /// <summary>
        /// 交易类型
        /// </summary>
        public string txnType { get; set; }

        /// <summary>
        /// 交易子类
        /// </summary>
        public string txnSubType { get; set; }

        /// <summary>
        /// 产品类型
        /// </summary>
        public string bizType { get; set; }

        /// <summary>
        /// 交易状态 
        /// <para>00/A6 交易成功</para>
        /// </summary>

        public string respCode { get; set; }

        /// <summary>
        /// 接入类型
        /// </summary>
        public string accessType { get; set; }

        /// <summary>
        /// 商户代码
        /// </summary>
        public string merId { get; set; }

        /// <summary>
        /// 订单号
        /// </summary>
        public string orderId { get; set; }

        /// <summary>
        /// 订单发送时间
        /// </summary>
        public string txnTime { get; set; }

        /// <summary>
        /// 交易金额
        /// </summary>
        public long? txnAmt { get; set; }

        /// <summary>
        /// 交易币种
        /// </summary>
        public string currencyCode { get; set; }

        /// <summary>
        /// 交易查询流水号
        /// </summary>
        public string queryId { get; set; }


        /// <summary>
        /// 响应信息
        /// </summary>
        public string respMsg { get; set; }

        /// <summary>
        /// 清算金额
        /// </summary>
        public long settleAmt { get; set; }

        /// <summary>
        /// 清算币种
        /// </summary>
        public int settleCurrencyCode { get; set; }

        /// <summary>
        /// 清算日期
        /// </summary>
        public string settleDate { get; set; }

        /// <summary>
        /// 系统跟踪号
        /// </summary>
        public string traceNo { get; set; }

        /// <summary>
        /// 交易传输时间
        /// </summary>
        public string traceTime { get; set; }

        /// <summary>
        /// 请求方保留域
        /// </summary>
        public string reqReserved { get; set; }

        /// <summary>
        /// 获取是否成功
        /// </summary>
        public bool IsSuccess()
        {
            return "00".Equals(this.respCode)
                    || "A6".Equals(this.respCode);
        }

        protected DateTime FormatTime(string time, string format)
        {
            return DateTimeUtils.GetDateTimeFormat(time, format);
        }
        /// <summary>
        /// 转换为查询结果
        /// </summary>
        /// <param name="channel">通知</param>
        /// <returns></returns>
        public ITradeOrderQueryResult ToPayResult(ITradeChannel channel)
        {
            if (string.IsNullOrWhiteSpace(this.orderId))
            {
                channel.ThrowChannelValidationException("银联交易付款通知的商户订单号为null或空白。");
            }
            if (string.IsNullOrWhiteSpace(this.queryId))
            {
                channel.ThrowChannelValidationException("银联交易付款通知的查询流水号为null或空白。");
            }
            if (this.txnAmt == null || this.txnAmt <= 0)
            {
                channel.ThrowChannelValidationException("银联交易交易付款通知总金额小于或等于零。");
            }
            if (this.traceTime == null)
            {
                channel.ThrowChannelValidationException("银联交易付款通知付款时间为 null。");
            }
            return new TradeOrderQueryResult(channel)
            {
                OrderNo = this.orderId,
                PayTime = DateTimeUtils.GetDateTimeFormat(this.traceTime, "MMddHHmmss"),
                TradeNo = this.queryId,
                OrderMoney = Convert.ToDecimal(this.txnAmt.Value) / 100M,
                Subject = "银联",
                Body = "银联",
                ExtraParam = reqReserved,
                PayFee = 0m,
                RefundFee = 0m,
                RefundMoney = 0m
            };
        }
    }
}
